package com.ruoyi.dl.wxPay;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

public class WechatUtil {

	/**
	 * 微信回调成功后 将 xml 转换为  WechatNotify 对象
	 * 
	 * @param request
	 * @return WechatNotify 对象
	 */
	public static WechatNotify getNotifyBean(HttpServletRequest request){
		try {
			DataInputStream in = new DataInputStream(request.getInputStream());
			byte[] dataOrigin = new byte[request.getContentLength()];
			// 根据长度，将消息实体的内容读入字节数组dataOrigin中
			in.readFully(dataOrigin); 
			// 关闭数据流
			in.close();
			// 从字节数组中得到表示实体的字符串
			String xml = new String(dataOrigin);
			// 将 xml 转换为  WechatNotify 对象
			Object object = XMLUtil.xmlToBean(WechatNotify.class, xml);
			if (object != null && object instanceof WechatNotify) {
				WechatNotify notify = (WechatNotify) object;
				return notify;
			} else {return null;}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	 /**
	  * 生成支付二维码
	  * @param request
	  * @param response
	  * @param width
	  * @param height
	  * @param text 微信生成预定id时，返回的codeUrl
	  */
	/* public static void getQRcode(HttpServletRequest request, HttpServletResponse response, Integer width, Integer height, String text) {
	  if (width == null) {
	   width = 300;
	  }
	  if (height == null) {
	   height = 300;
	  }
	  String format = "jpg";
	  Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
	  hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
	  BitMatrix bitMatrix;
	  try {
	   bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
	   MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream());
	  } catch (WriterException e) {
	   e.printStackTrace();
	  } catch (IOException e) {
	   e.printStackTrace();
	  }
	 }*/
}